package com.pingan.haofang.searchcloud.web.monitor.validator;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.pingan.haofang.framework.common.functional.lang.control.validator.bo.ValidationError;
import com.pingan.haofang.framework.common.functional.lang.control.validator.bo.ValidationResult;
import com.pingan.haofang.searchcloud.common.form.PageForm;
import com.pingan.haofang.searchcloud.common.utils.DateUtils;
import com.pingan.haofang.searchcloud.common.validator.annotation.ValidHandler;
import com.pingan.haofang.searchcloud.web.monitor.form.DashboardApiSummaryInfoForm;
import com.pingan.haofang.searchcloud.web.monitor.form.DashboardApiSummaryInfoFormByList;
import com.pingan.haofang.searchcloud.web.monitor.form.DashboardIndexSummaryInfoForm;

/**
 * 搜索云预警Form校验器
 *
 * @author EX-ZHOUYONG005
 * @date 2018/11/7
 * @since 1.0.0
 */
@Component
public class DashboardMonitorValidator {

    //索引视图-(列表分页)
    @ValidHandler
    public void indexViewByPage(ValidationResult result, DashboardIndexSummaryInfoForm form, PageForm pageForm) {
        handle(result, form.getStartDate(), form.getEndDate());
    }

    //索引视图-(图表)
    @ValidHandler
    public void indexViewByChart(ValidationResult result, DashboardIndexSummaryInfoForm form) {
        handle(result, form.getStartDate(), form.getEndDate());
    }


    //索引_选定项目视图-(列表分页)
    @ValidHandler
    public void indexViewByProjectOfPage(ValidationResult result, DashboardIndexSummaryInfoForm form,
                                         PageForm pageForm) {
        handle(result, form.getStartDate(), form.getEndDate());
    }

    //索引_选定项目视图-(图表)
    @ValidHandler
    public void indexViewByProjectOfChart(ValidationResult result, DashboardIndexSummaryInfoForm form) {
        handle(result, form.getStartDate(), form.getEndDate());
    }

    //索引_选定索引视图-(图表+列表分页)
    @ValidHandler
    public void indexViewByIndex(ValidationResult result, DashboardIndexSummaryInfoForm form, PageForm pageForm) {
        handle(result, form.getStartDate(), form.getEndDate());
    }

    /**
     * API视图
     */
    @ValidHandler
    public void getTablePart(ValidationResult result, DashboardApiSummaryInfoForm infoForm, PageForm form) {
        handle(result, infoForm.getStartDate(), infoForm.getEndDate());
    }

    /**
     * API视图
     */
    @ValidHandler
    public void getChartPart(ValidationResult result, DashboardApiSummaryInfoFormByList form) {
        handle(result, form.getStartDate(), form.getEndDate());
    }


    private void handle(ValidationResult result, String startDate, String endDate) {
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            Date start = null;
            Date end = null;
            try {
                start = DateUtils.parseDate(startDate, "yyyy-MM-dd");
                end = DateUtils.parseDate(endDate, "yyyy-MM-dd");
            } catch (ParseException e) {
                result.addError(ValidationError.of("dateError", "dashboard.monitor.date.fomate.error"));
            }
            // 开始时间不能大于结束时间
            if (start.after(end)) {
                result.addError(ValidationError.of("endDate", "dashboard.monitor.date.range.error"));
            }
            // 结束时间不能大于开始时间一年
            if (DateUtils.getBetweenDays(start, end) > 30) {
                result.addError(ValidationError.of("endDate", "dashboard.monitor.date.range.out"));
            }
        }
    }
}
